package com.cdsz.boot.common.utils;

import java.util.Arrays;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author gongli
 * @date 2024/6/6 16:49
 */
public class TraceIdUtils {
    private static final ThreadLocal<String> TRACE_ID_HOLDER = new ThreadLocal<>();

    public static String getTraceId() {
        if(TRACE_ID_HOLDER.get() == null){
            TRACE_ID_HOLDER.set(genTraceId());
        }
        return TRACE_ID_HOLDER.get();
    }

    public static String genTraceId() {
        String uuid = UUID.randomUUID().toString();
        return Arrays.stream(uuid.split("-")).limit(2).collect(Collectors.joining());
    }
}
